<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The PhotoNews class for table 'photonews'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2) Inheriting from BaseNews instead of Original DataObject.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_basenews.php';

require_once INSTALLDIR . '/classes/News.php';
require_once INSTALLDIR . '/classes/PhotoSet.php';
require_once INSTALLDIR . '/classes/Photo.php';

// Definitions.
// Column of status reserves.

class PhotoNews extends BaseNews
{
	public $__table = 'photonews';			// table name
	public $id;								// int(11)  primary_key not_null
	public $news_id;						// int(11)  foreign_key unique not_null
	public $user_id;						// int(11)  foreign_key not_null
	public $photoset_id;					// int(11)  foreign_key not_null
	public $photo_ida;						// int(11)  foreign_key not_null
	public $photo_idb;						// int(11)  foreign_key
	public $photo_idc;						// int(11)  foreign_key
	public $created;						// datetime  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('PhotoNews', $key, $value); 
	}
	
	/**
	 * Get the photo set title.
	 */
	function getPhotosetTitle()
	{
		$query = 'SELECT `title` FROM `photoset`' .
				 'WHERE `id` = ' . $this->photoset_id;

		$photoset = new PhotoSet();
		$photoset->query($query);
		
		$result = '';
		if ($photoset->fetch()) {
			$result = $photoset->title;
		}
		
		$photoset->free();
		$photoset = null;
		
		return $result;
	}
	
	/**
	 * Add the photo news into database.
	 * 
	 * $fields includes: $photoset_id, $user_id.
	 */
	static function add($fields)
	{
		// Magically put fields into current scope.
		extract($fields);
		
		$news = new News();
		$news->newstype = NEWS_NEWSTYPE_PHOTONEWS;
// TODO: The other variables.
		$news->newstag_id = 0;
		$news->status = 0;
		
		$news_id = $news->insert();
		if (empty($news_id)) {
			slh_log_db_error($news, 'INSERT', __FILE__);
			return false;
		}
		$news->id = $news_id;
		
		$photonews = new PhotoNews();
		$photonews->news_id = $news_id;
// FIXME: The user id.
		$photonews->user_id = 0;
		
		$photonews->photoset_id = $photoset_id;
		// Get the fresh photo ids.
		$ids = PhotoSet::getFreshPhotoIds($photoset_id, 3);
		$photonews->photo_ida = $ids[0];
		$photonews->photo_idb = $ids[1];
		$photonews->photo_idc = $ids[2];
		
		$photonews->created = slh_sql_now();
		
		$photonews_id = $photonews->insert();
		if (empty($photonews_id)) {
			slh_log_db_error($photonews, 'INSERT', __FILE__);
			
			@$news->delete();
			return false;
		}
		
		return $photonews_id;
	}
	
	/**
	 * Check if could post a newly photo news of the same photo set
	 * within the allowed interval.
	 */
	static function canPostWithinInterval($photoset_id)
	{
		$postinterval = slh_config('photonews', 'postinterval');
		
		if ( !is_null($postinterval)) {
			$photonews = new PhotoNews();
			$photonews->selectAdd();
			$photonews->selectAdd('created');
			$photonews->whereAdd('photoset_id = ' . $photoset_id);
			$photonews->orderBy('created DESC');
			$photonews->limit(0, 1);
			
			/*
			$query = 'SELECT `created` FROM `photonews`' .
					 ' WHERE `photoset_id` = ' . $photoset_id .
					 ' ORDER BY created DESC' .
					 ' LIMIT 0, 1';
			$photonews->query($query);
			*/
			
			if ($photonews->find()) {
				// Get the update time to check the interval is validated.
				if ($photonews->fetch()) {
					$created = $photonews->created;
					$lastdatetime = strtotime($created);
					$interval = time() - $lastdatetime;		//unit: s 
	
					if ($interval <= intval(slh_config('photonews', 'postinterval'))) {
						// Posting is denied.
						return false;
					}
				}
			}
		}
		
		return true;
	}
	
	/**
	 * Get the ids as a list. Use offset and limit to control
	 * the pagination.
	 * 
	 * Returning the 'news_id', not the original 'id'.
	 */
	static function getNewsidsByList($offset = 0,
									 $limit = 0)
	{
		return parent::getNewsidsByList(new self(), $offset, $limit);
	}
	
	/**
	 * Get the photo news objects by the given newsids.
	 */
	static function getPhotonewsByNewsids($newsids)
	{
		return parent::getSubnewsByNewsids('PhotoNews', $newsids);
	}
}
?>